package com.zlsy.thread.threadpool;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author zhouliang
 * @date 2020/7/1 8:36
 */
public class SingleThreadExecutorTest {

    /**
     *可以看出，这里和刚才的newFixedThreadPool的原理基本一致，只不过是把核心线程数设置成为了1，
     * 所以这也会导致同样的问题。也就是请求堆积的时候，可能会占用大量的内存。
     */
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 100; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(new Random().nextInt(3000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
        executorService.shutdown();
    }
}
